package com.zjkj.mp.service.finance;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjkj.mp.dao.finance.FinanceItemDAO;
import com.zjkj.mp.dao.user.UserDao;
import com.zjkj.mp.utils.PaginatedResult;
import com.zjkj.mp.vo.finance.FinanceItem;
import com.zjkj.mp.vo.finance.FinanceItemType;
import com.zjkj.mp.vo.user.MpUser;

@Service
public class FinanceItemServiceImpl implements FinanceItemService{

	@Autowired
	FinanceItemDAO itemDAO;
	@Autowired
	UserDao userDAO;
	
	@Override
	public void addItem(FinanceItem item) {
		itemDAO.addItem(item);
	}

	@Override
	public PaginatedResult getList(int pageNo, int pageSize,
			Map<String, Object> params) {
		if(params == null) params = new HashMap<String, Object>();
		
		return this.itemDAO.findList(pageNo, pageSize, params);
	}

	@Override
	public void sendMessage(long count) {
		FinanceItem item = new FinanceItem();
		item.setChangeCount(0-count);
		item.setOperateDate(new Date());
		item.setOperateType(FinanceItemType.SEND_MESSAGE);
		
		//得到当前登录用户
		MpUser user = MpUser.getCurrentUser();
		item.setUserId(user.getUserId());	
		item.setUserName(user.getUsername());
		
		//重新查询数据库 得到余额信息	
		item.setBalance(userDAO.getBalance(user.getUserId()));
		
		this.addItem(item);
		
	}

}
